package com.funkids.security;

import com.funkids.prxis.entity.SysUser;
import com.funkids.prxis.service.ISysUserService;
import com.funkids.prxis.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {
    
    @Autowired
    ISysUserService sysUserService;
    
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = sysUserService.getByUsername(username);
        if (sysUser == null) {
            throw new UsernameNotFoundException("用户名或密码不正确");
        }
        return new AccountUser(sysUser.getUserId(), sysUser.getUserName(), sysUser.getPassword(),getUserAuthority(sysUser.getUserId()));

    }
    /**
     * 获取用户权限信息（角色、菜单权限）
     * @param userId
     * @return
     */
    public List<GrantedAuthority> getUserAuthority(Long userId){

        // 角色(ROLE_admin)、菜单操作权限 sys:user:list
        String authority = sysUserService.getAuthorityInfoByid(userId);  // ROLE_admin,ROLE_normal,sys:user:list,....
        //System.out.println(userId+"->"+authority);

        return AuthorityUtils.commaSeparatedStringToAuthorityList(authority);
    }
}
